#!/usr/bin/env bash

set -o errexit

if [[ -t 1 ]]
then
    exec 2>&1 | ts '%X %Z'
fi

exec 220>/tmp/.RunSolrBackup.lock || exit 1
# Will be automatically released when the script exits.
flock -n 220 || { echo "Failed to obtain lock. Another instance is running?" >&2; exit 1; }

# This is to help with disk space monitoring - run "df" before and after
echo "Disk space when RunSolrBackup starts:" ; df -m
trap 'echo "Disk space when RunSolrBackup ends:" ; df -m' 0

MB_SERVER_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../" && pwd)
cd "$MB_SERVER_ROOT"

source admin/config.sh

. ./admin/functions.sh
make_temp_dir

DUMP_DIR="$SOLR_BACKUP_DIR"
DUMP_STAMP=`TZ=UTC date +'%Y%m%d-%H%M%S'`

# Create necessary directories and set permissions.
mkdir -m "$SOLR_BACKUP_DIR_MODE" -p "$DUMP_DIR/$DUMP_STAMP"
chown "$SOLR_BACKUP_USER:$SOLR_BACKUP_GROUP" \
    "$DUMP_DIR" \
    "$DUMP_DIR"/"$DUMP_STAMP"

echo Requesting SolrCloud backups
BACKUP_STAMP="$DUMP_STAMP" \
    ./admin/RequestSolrCloudBackups \
    || exit $?

echo Retrieving SolrCloud backups
MBS_ADMIN_CONFIG=config.solr-backup.sh \
    ./bin/rsync-solrcloud-backups "$TEMP_DIR" \
    || exit $?

echo Bundling Solr backups
BACKUP_STAMP="$DUMP_STAMP" \
    ./admin/BundleSolrBackup \
    --working-dir "$TEMP_DIR" \
    || exit $?

echo Copying the dump to the local FTP directory
chown "$SOLR_BACKUP_USER:$SOLR_BACKUP_GROUP" "$TEMP_DIR"/*
chmod "$SOLR_BACKUP_FILE_MODE" "$TEMP_DIR"/*
mv "$TEMP_DIR"/* "$DUMP_DIR"/"$DUMP_STAMP"/

# Finally create a "latest-is" file, indicating the export we just did.
rm -rf "$DUMP_DIR"/latest-is-*
> "$DUMP_DIR"/latest-is-"$DUMP_STAMP"
chmod "$SOLR_BACKUP_FILE_MODE" "$DUMP_DIR"/latest-is-"$DUMP_STAMP"
chown "$SOLR_BACKUP_USER:$SOLR_BACKUP_GROUP" "$DUMP_DIR"/latest-is-"$DUMP_STAMP"

# Finally finally, create a LATEST file whose *contents* are this export's tag
rm -rf "$DUMP_DIR"/LATEST
echo "$DUMP_STAMP" > "$DUMP_DIR"/LATEST
chmod "$SOLR_BACKUP_FILE_MODE" "$DUMP_DIR"/LATEST
chown "$SOLR_BACKUP_USER:$SOLR_BACKUP_GROUP" "$DUMP_DIR"/LATEST

echo Deleting old full exports from the local FTP directory
./bin/delete-old-fullexports -k -r "$DUMP_DIR"

echo Syncing the local FTP directory with the FTP server
MBS_ADMIN_CONFIG=config.solr-backup.sh ./bin/rsync-fullexport-files
